package com.framework.loippi.controller.api;

import com.framework.loippi.api.utils.ApiUtils;
import com.framework.loippi.entity.DoctorWork;
import com.framework.loippi.service.DoctorWorkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Controller - 医生坐班表
 *
 * @author wmj
 * @version 2.0
 */
@Controller("apiWorkController")
@RequestMapping("/api/work")
@Api(tags = {"获取医生坐班信息"})
public class WorkController extends ApiBaseController {
   @Resource
   DoctorWorkService workService;

   @RequestMapping(value = "/getDoctorWork.json", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
   @ApiOperation(value = "根据科室id获取医生排班信息", notes = "{}")
   public @ResponseBody
    String getDoctorWork(@Param(value="departmentId") String departmentId) {
       List<DoctorWork> list = workService.findWorkByDoctorId(Integer.valueOf(departmentId));
       List<DoctorWork> mornList = new ArrayList<>();
       List<DoctorWork> noonList = new ArrayList<>();
       List<Map<String,Object>> listMap = new ArrayList<>();
       Map<String, Object> dataMap = new HashMap<>();
       for (DoctorWork s : list) {
          if (s.getWorkTime() == 0) {
             mornList.add(s);
          } else {
             noonList.add(s);
          }
       }
       dataMap.put("mornList", mornList);
       dataMap.put("noonList", noonList);
       listMap.add(dataMap);
       return ApiUtils.success(listMap);
   }

   @RequestMapping(value = "getSubscribeTime.json", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
   @ApiOperation(value = "根据日期查询医生排班时间", notes = "{}")
   public @ResponseBody
   String getSubscribeTime(@Param("workDate")String workDate, @Param("doctorId")Integer doctorId) throws ParseException {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      Date wd = sdf.parse(workDate);
      List<String> times = workService.findSubscribeTime(wd, doctorId);

      return ApiUtils.success(times);
   }
}
